package com.zxs.domain.handel.subject;

import com.zxs.common.entity.Result;
import com.zxs.common.enums.SubjectInfoTypeEnum;
import com.zxs.domain.entity.dto.SubjectInfoDTO;
import com.zxs.infra.basic.entity.SubjectJudge;
import com.zxs.infra.basic.entity.SubjectMultiple;
import com.zxs.infra.basic.entity.SubjectRadio;
import com.zxs.infra.basic.mapper.SubjectMappingMapper;
import com.zxs.infra.basic.service.SubjectMappingService;
import com.zxs.infra.basic.service.impl.SubjectJudgeServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 判断的题目的策略类
 * @author zxs
 * @date 2025/11/16 上午4:07
 */
@Component
public class JudgeTypeHandel implements SubjectTypeHandel{

    @Autowired
    private SubjectJudgeServiceImpl subjectJudgeService;

    @Autowired
    private SubjectMappingMapper subjectMappingMapper;

    @Override
    public SubjectInfoTypeEnum getHandelType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public Result add(SubjectInfoDTO subjectInfoDTO) {
        //判断题目的插入
        Result<Object> result = new Result<>();
        List<SubjectJudge> subjectJudges = new ArrayList<>();
        //单选题目的插入
        if (CollectionUtils.isEmpty(subjectInfoDTO.getSubjectAnswerDTOS())) {
            result = Result.failed("选项不能为空！");
            return result;
        }
        subjectInfoDTO.getSubjectAnswerDTOS().forEach(option -> {
            SubjectJudge subjectJudge = new SubjectJudge();
            BeanUtils.copyProperties(option, subjectJudge);
            subjectJudge.setSubjectId(subjectInfoDTO.getId());
            subjectJudges.add(subjectJudge);
        });

        Integer i = subjectJudgeService.batchInsert(subjectJudges);
        if (i == subjectJudges.size()) {
            result =Result.success("插入判断题成功");
            return result;
        }
        return result;
    }

    @Override
    public SubjectInfoDTO query(int subjectId) {
        SubjectInfoDTO subjectInfoDTO = new SubjectInfoDTO();
        //查找正确答案
        SubjectJudge subjectJudge = subjectJudgeService.queryBySubjectId(subjectId);
        if (subjectJudge.getIsCorrect() == 1) {
            //该题目正确
            subjectInfoDTO.setSubjectAnswer("正确");
        }

        List<String> labelNames = subjectMappingMapper.selectLabelNames(subjectId);
        subjectInfoDTO.setLabelNames(labelNames);
        return subjectInfoDTO;
    }
}